package com.duoec.common.plus;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;

/**
 * @author xuwenzhen
 * @date 2020/1/6
 */
public interface PojoPlus {
    /**
     * 获取实例，此实例会包含父级的数据
     *
     * @param clazz 需要初始化的实体类
     * @return 某个类的实体
     */
    default <T extends PojoPlus> T fork(Class<T> clazz) {
        ClassPool cp = ClassPool.getDefault();
        CtClass cc;
        CtMethod m;

        try {
            cc = cp.get(clazz.getName());
            m = cc.getDeclaredMethod("getSalary");
            m.insertBefore("{ System.out.println(\"Hello.say():\"); }");
            Class c = cc.toClass();

            return (T) getT(c);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    default <T extends PojoPlus> T getT(Class<T> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
